package com.example.hrandroidapp.adapter;

import java.util.ArrayList;

import org.w3c.dom.ls.LSOutput;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import com.example.hrandroidapp.R;
import com.example.hrandroidapp.domain.LeaveType;
import com.example.hrandroidapp.domain.TaskType;

public class CustomSpinnerAdapter extends ArrayAdapter {
	
	private Context context;
	private ArrayList<LeaveType> lstLeaveType;
	private ArrayList<TaskType> lstTaskType;
	private boolean isOT = false;

	public CustomSpinnerAdapter(Context context, int spinnerViewResource, ArrayList<LeaveType> lstLeaveType) {
		super(context, spinnerViewResource, lstLeaveType);
		this.context=context;
		this.lstLeaveType=lstLeaveType;
	}
	
	public CustomSpinnerAdapter(Context context, int spinnerViewResource, ArrayList<TaskType> lstTaskType, boolean isOT) {
		super(context, spinnerViewResource, lstTaskType);
		this.context = context;
		this.lstTaskType = lstTaskType;
		this.isOT=isOT;
	}
	
	@Override
	public View getDropDownView(int position, View convertView, ViewGroup parent) {
		return getCustomView(position, convertView, parent);
	}
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		return getCustomView(position, convertView, parent);
	}
	
	@Override
	public Object getItem(int position) {
		if(isOT) 
			return lstTaskType.get(position);
		else 
			return lstLeaveType.get(position);
	}
	
	public View getCustomView(int position, View convertView, ViewGroup parent) {
		LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View row = null; 
		
		if (convertView == null) {

            row = new View(context);
            row = inflater.inflate(R.layout.leave_type_spinner, parent, false);

            TextView tvLeaveType = (TextView) row.findViewById(R.id.tv_leave_type);
            
            if(isOT) {
            	TaskType tmpTaskType = lstTaskType.get(position);
            	tvLeaveType.setText(tmpTaskType.getDescription());
            }else {
	    		LeaveType tmpLeaveType = lstLeaveType.get(position);
	    		tvLeaveType.setText(tmpLeaveType.getName());
            }
        } else {
            row = (View) convertView;
        }
		
		return row;
	}
	
}
